package com.qf.ayc.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@Component
@ServerEndpoint("/hello/{name}")
public class MyWebSocketServer {
    //当浏览器跟我们建立连接时，我们做什么
    @OnOpen
    public void onOpen(Session session, @PathParam("name") String name) throws IOException {

        System.out.println(name+"连入");
        session.getBasicRemote().sendText("hello:"+name);//服务器端连接浏览器
    }
    // 当有消息送到服务 器端时，想F什么
    @OnMessage
    public void onMsg(Session session, @PathParam("name") String name,String msg) throws IOException {
        System.out.println("收到的消息为："+msg);
        session.getBasicRemote().sendText("来自服务器端的问候："+msg);

    }
    @OnClose
    //当浏览器断开连接后，想干什么
    public void onClose(Session session, @PathParam("name") String name) throws IOException {
        System.out.println(name+"分别了");
        session.close();
    }
}
